/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.integral.web.controller.api;


import com.medusa.gruul.common.core.annotation.EscapeLogin;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.core.util.CurUserUtil;
import com.medusa.gruul.common.core.util.Result;
import com.medusa.gruul.common.core.util.StringUtil;
import com.medusa.gruul.common.core.util.SystemCode;
import com.medusa.gruul.common.dto.CurUserDto;
import com.medusa.gruul.integral.service.IStepNumberService;
import com.medusa.gruul.integral.web.model.dto.manager.DecryptRunData;
import com.medusa.gruul.integral.web.model.dto.manager.UserStepNumberDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *   步数表 前端控制器
 * </p>
 *
 * @author xiaoq
 * @since 2019-11-19
 */
@RestController
@RequestMapping("/stepNumber")
@Api(tags = "用户步数相关接口")
public class StepNumberController {

	@Autowired
	private IStepNumberService stepNumberService;

	/**
	 * 解密微信运动步数
	 *
	 * @param decryptRunData 微信运动数据
	 *
	 * @return 用户实时步数
	 */
	@PostMapping("decrypt")
	@ApiOperation(value = "解密获取用户当天的微信运用步数以及相关数据")
	public Result getUserStepNumber(@RequestBody @Validated DecryptRunData decryptRunData) {
		String userStepNumber = stepNumberService.getUserStepNumber(decryptRunData);
		return Result.ok(userStepNumber);
	}


	/**
	 * 用户进行步数兑换
	 *
	 * @param todayStep 用户当前步数
	 * @param targetStep 后台设置的目标步数
	 * @return 用户剩余步数
	 */
	@GetMapping("conversion")
	@ApiOperation(value = "用户进行步数兑换")
	@EscapeLogin
	public Result stepNumberConversion(@ApiParam(value = "用户当前步数", required = true) Integer todayStep,
			@ApiParam(value = "用户目标步数", required = true) Integer targetStep) {
		if (todayStep < targetStep) {
			throw new ServiceException("步数不足无法进行兑换");
		}

		String stepNumber = stepNumberService.userStepNumberConversion(todayStep, targetStep);
		return Result.ok(stepNumber);
	}


	/**
	 * 用户步数兑换相关信息获取
	 * @param theCurrentStep 用户当前步数
	 *
	 * @return 用户兑换步后的实时 UserStepNumberDto 数据
	 */
	@GetMapping("conversionInfo")
	@ApiOperation(value = "用户步数兑换相关信息获取")
	public Result<UserStepNumberDto> userStepNumberInfo(
			@ApiParam(value = "用户当前步数", required = true) Integer theCurrentStep) {
		CurUserDto feignCurUser = CurUserUtil.getHttpCurUser();
		String userId = feignCurUser.getUserId();
		if (StringUtil.isEmpty(userId)) {
			throw new ServiceException("数据错误", SystemCode.DATA_NOT_EXIST.getCode());
		}
		UserStepNumberDto userStepNumberInfo = stepNumberService.getUserStepNumberInfo(userId, theCurrentStep);
		return Result.ok(userStepNumberInfo);
	}
}
